<?php
namespace My\Controller;

use Utuo\BackgroundController;

class AgentApplyController extends BackgroundController {
	
	protected function _initialize() {
		$this->setControllerTitle('代理商申请管理');
		$this->setActionTitle(array(
			'index' => '代理商申请列表',
		));
		$this->setActionNav(array(
			'index',
		));
	}
	
	/**
	 * 申请列表
	 * @param unknown $is_deleted
	 */
	public function getLists( $is_deleted ) {
		$realName = I("realName", "");
		$tel = I("tel", "");
		
		$params = array();
		$realName and $params['realName'] = $realName;
		$tel and $params['tel'] = $tel;
		
		$condition = array(
			'a.cid' => $this->aid,
			'b.cid' => $this->aid,
		);
		$realName and $condition['a.realName'] = $realName;
		$tel and $condition['a.tel'] = $tel;
		
		$d = D('AgentApply a')->join("__WECHAT_USER__ b on a.openId=b.openId", "LEFT")->where($condition)
							  ->field("a.*,b.id as uid,b.type,b.nickname");
		$n = clone $d;
		$agentApplyLists = $d->page($this->getPage(), $this->getPageSize())->order("a.createTime desc")->select();
		$count = $n->count();
		$this->assign("params", $params);
		$this->showPage($count, $this->getPageSize());
		$this->assign('agentApplyLists', $agentApplyLists);
		$this->display();
	}
	
	/**
	 * 加为代理商
	 */
	public function addToAgentAction() {
		$id = I('id', 0, "intval");
		$agentApply = M("AgentApply a")->join("__WECHAT_USER__ b on a.openId=b.openId", "LEFT")->where('a.cid=%d and b.cid=%d and a.id=%d', $this->aid, $this->aid, $id)
									   ->field("a.*,b.id as uid,b.type,b.nickname")->find();
		if ( $agentApply ) {
			if ( !$agentApply['uid']  ) {
				exit('未找到申请用户');
			} else {
				redirect(U('Agent/add', "wechat_uid={$agentApply['uid']}&applyId=$id"));
			}
		}
	}
	
}